//
//  BKXianShangShengQingView.m
//  SanYiTeaching
//
//  Created by 李文强 on 2019/4/24.
//  Copyright © 2019 SanYi. All rights reserved.
//

#import "BKXianShangShengQingView.h"

@interface BKXianShangShengQingView ()<UITableViewDelegate,UITableViewDataSource>
{
    /*
     0 全不选
     1 全部选
     2 取消按钮状态
     */
    int selectedAll;
    
}
@property (nonatomic, strong) NSMutableArray *selectedEmuArray;
@property (nonatomic, strong) UIButton *checkResourceButton;
@property (nonatomic, strong) SYNoLabel *nolab;
@property (nonatomic, strong) NSMutableArray *listDataArr;
@end

@implementation BKXianShangShengQingView

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    _selectedEmuArray = [NSMutableArray new];
    // Do any additional setup after loading the view.
    _nolab = [[SYNoLabel alloc] initWithNameStr:@"" contView:self.view];
}

- (void)requestDataArr:(NSMutableArray *)listArr {
    if (listArr.count == 0) {
        _nolab.hidden = NO;
        self.tableList.hidden = YES;
    } else {
        self.tableList.hidden = NO;
        _nolab.hidden = YES;
        self.listDataArr = listArr;
        [self.tableList reloadData];
    }
}


- (void)initSubView{
    selectedAll = 2;
    keyWord = @"";
    [self.view addSubview:self.searchView];
//    [_searchView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(@0);
//        make.right.equalTo(@0);
//        make.top.equalTo(@0);
//        make.height.equalTo(@100);
//    }];
    [self.view addSubview:self.tableList];
    [_tableList mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.top.equalTo(@0);
        make.bottom.equalTo(@0);
    }];
    [self.view addSubview:self.checkResourceButton];
    [_checkResourceButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.mas_centerX);
        make.height.equalTo(@50);
        make.width.equalTo(@100);
        make.bottom.equalTo(@0).offset(-10);
    }];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(selectedAllClick:) name:@"BKGLTVTitleViewSelectedAllNotice" object:nil];
    
}
- (UIButton *)checkResourceButton{
    if (!_checkResourceButton) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:@"选择资源" forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageWithColor:CONTENTBACKCOLOR forSize:CGSizeMake(100, 100)] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:16];
        [button setTitleColor:SELECTEDCOLOR forState:0];
        button.layer.cornerRadius = 5;
        button.layer.masksToBounds = YES;
        button.layer.borderColor = SELECTEDCOLOR.CGColor;
        button.layer.borderWidth = 1;
        [button addTarget:self action:@selector(checkResourceButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        button.hidden = YES;
        _checkResourceButton = button;
    }
    return _checkResourceButton;
}
- (void)checkResourceButtonClick:(UIButton *)sender{
    NSMutableArray *applyids = [NSMutableArray array];
//    TOP
    NSString *applyid = nil;
    NSString *courseid = nil;

    for (NSString *index in _selectedEmuArray) {
        SYBuKeGuLiModel *bkgl = self.listDataArr[[index integerValue]];
        if (applyid.length == 0) {
            applyid = bkgl.bkid;//补课申请
            courseid = bkgl.courseId;
        } else {
            applyid = SYStringWithPickFormat(applyid, SYStringWithPickFormat(@",", bkgl.bkid));
            courseid = SYStringWithPickFormat(courseid, SYStringWithPickFormat(@",", bkgl.courseId));
        }

    }

    [applyids addObject:applyid];
    [applyids addObject:courseid];
    
    [self.mainDelegate goFaFangZiYuanView:applyids];
}
- (void)selectedAllClick:(NSNotification *)info{
    int ststus = [[info.userInfo objectForKey:@"status"] intValue];
    if (ststus == 0) {
        //取消全部
        selectedAll = 0;
    }else{
        //选择全部
        selectedAll = 1;
    }
    [_tableList reloadData];
}
- (void)layout{
    
}
- (UITableView *)tableList{
    if (!_tableList) {
        UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, APP_HEIGHT-84) style:UITableViewStylePlain];
        tableView.delegate  =self;
        tableView.dataSource = self;
        tableView.backgroundColor = [UIColor whiteColor];
        [tableView registerClass:[BKGLTableViewCell class] forCellReuseIdentifier:@"BKGLTableViewCell"];
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableList = tableView;
    }
    return _tableList;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //TOP
    return self.listDataArr.count;
//    return 1;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    BKGLTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BKGLTableViewCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[BKGLTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"BKGLTableViewCell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.row%2 == 0) {
        cell.backgroundColor = [UIColor whiteColor];
    }else{
        cell.backgroundColor = [UIColorChange colorwithHexString:@"#F8F8F8"];
    }
    //TOP
    SYBuKeGuLiModel *bkgl = self.listDataArr[indexPath.row];
    cell.dataArray = @[bkgl.serial,bkgl.name,bkgl.courseName,bkgl.lesson,@"线上",bkgl.attendDate,@""];
    

    cell.indexPath = indexPath;
    [cell reloadViewLayout];

    //记录选择状态
    if ([_selectedEmuArray containsObject:[NSString stringWithFormat:@"%ld",(long)indexPath.row]]) {
        cell.selectedAllButton.selected = YES;
    } else {
        cell.selectedAllButton.selected = NO;
    }
    //点击全选按钮逻辑处理
    if (selectedAll == 0) {
        cell.selectedAllButton.selected = NO;
        [_selectedEmuArray removeAllObjects];
        [self addButtonFunction];
    }else if (selectedAll == 1){
        cell.selectedAllButton.selected = YES;
        [_selectedEmuArray addObject:[NSString stringWithFormat:@"%ld",(long)indexPath.row]];
        [self addButtonFunction];
    }else{
        
    }
    
    //显示按钮
    __block BKXianShangShengQingView *weakSelf = self;
    cell.block = ^(NSIndexPath *index) {
        [self->_selectedEmuArray removeAllObjects];
        [self->_selectedEmuArray addObject:[NSString stringWithFormat:@"%ld",(long)index.row]];
        self->selectedAll = 2;
        [weakSelf addButtonFunction];
        [weakSelf->_tableList reloadData];
    };
    cell.cancelBlock = ^(NSIndexPath *index) {
        [self->_selectedEmuArray removeObject:[NSString stringWithFormat:@"%ld",(long)index.row]];
        self->selectedAll = 2;
        [weakSelf addButtonFunction];
    };
    //是否已近选择按钮
    
    return cell;
}
- (void)addButtonFunction{
   // NSLog(@"数组里的元素 %@",_selectedEmuArray);
    if (_selectedEmuArray.count>0) {
        _checkResourceButton.hidden = NO;
    }else{
        _checkResourceButton.hidden = YES;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
   // NSLog(@"点击了cell");
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (!_titleView) {
        _titleView = [[BKGLTVTitleView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44) andArray:@[@"学号",@"姓名",@"教材",@"课时",@"类型",@"申请时间",@"选择"]];
        _titleView.backgroundColor = CONTENTBACKCOLOR;
    }
    _titleView.frame = CGRectMake(0, 0, self.view.frame.size.width, 44);
    [_titleView reloadView];
    return _titleView;
}
/*
 如果全选了之后或者有选择就弹出选择资源
 */
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *mainView = [[UIView alloc]init];
    return mainView;
}
- (BKXianShangSearchView *)searchView{
    if (!_searchView) {
        BKXianShangSearchView *view = [[BKXianShangSearchView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 100)];
        view.backgroundColor = [UIColor whiteColor];
        //添加监听
        [view.searchView.searchTF addTarget:self action:@selector(textFieldTextDidChange:) forControlEvents:UIControlEventEditingChanged];
        
        view.searchView.block = ^(NSString * _Nonnull keyStr) {
           
        };
        
        _searchView = view;
    }
    return _searchView;
}
- (void)textFieldTextDidChange:(UITextField *)textfile{
    keyWord = textfile.text;
}
/*   
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"BKGLTVTitleViewSelectedAllNotice" object:nil];
}
@end
